/**
 * 
 * @Project le cOeur - Data Virtualization
 * @Date 08.12.2012
 * @Author Aebi Pascal, Kocher Marc
 * @School BFH-TI Biel, Bachelor Thesis
 *
 */

package main.framework;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

import main.framework.http.MessageBody;
import main.framework.http.MessageHeader;
import main.framework.http.ReasonPhrase;
import main.framework.http.Response;
import main.framework.http.StatusCode;
import main.framework.http.Version;

/**
 * This class is able to create different HTTP responses.
 */
public class ResponseFactory {
	
	private SimpleDateFormat simpleDateFormat;

	/**
	 * Creates a response factory.
	 */
	public ResponseFactory() {
		this.simpleDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
		this.simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
	}
	
	/**
	 * Creates a HTTP response corresponding to the given parameters.
	 * It adds automatically the predefined message headers "Date", "Cache-Control" and "Connection".
	 * 
	 * @param version The HTTP version.
	 * @param statusCode The HTTP status code.
	 * @param reasonPhrase The HTTP reason phrase.
	 * @return The corresponding HTTP response.
	 */
	public Response createResponse(Version version, StatusCode statusCode, ReasonPhrase reasonPhrase) {
		Response response = new Response(version, statusCode, reasonPhrase);
		this.addDefaultMessageHeaders(response);
		return response;
	}
	
	/**
	 * Creates a HTTP response corresponding to the given parameters.
	 * It adds automatically the predefined message headers "Date", "Cache-Control" and "Connection".
	 * In addition corresponding to the message body the message headers "Content-Type" and "Content-Length" are added too.
	 * 
	 * @param version The HTTP version.
	 * @param statusCode The HTTP status code.
	 * @param reasonPhrase The HTTP reason phrase.
	 * @param messageBody The HTTP message body.
	 * @return The corresponding HTTP response.
	 */
	public Response createResponse(Version version, StatusCode statusCode, ReasonPhrase reasonPhrase, MessageBody messageBody) {
		Response response = new Response(version, statusCode, reasonPhrase);
		this.addDefaultMessageHeaders(response);
		response.addMessageBody(messageBody);
		response.addMessageHeader(new MessageHeader("Content-Type", messageBody.getContentType().toString()));
		response.addMessageHeader(new MessageHeader("Content-Length", Integer.toString(messageBody.getLength())));
		return response;
	}
	
	/**
	 * Creates a HTTP response corresponding to the given parameters.
	 * It adds automatically the predefined message headers "Date", "Cache-Control" and "Connection".
	 * The given list of message headers is added after the predefined message headers.
	 * Thereby already existing headers will be overwritten.
	 * 
	 * @param version The HTTP version.
	 * @param statusCode The HTTP status code.
	 * @param reasonPhrase The HTTP reason phrase.
	 * @param messageHeaders A list of message headers.
	 * @return The corresponding HTTP response.
	 */
	public Response createResponse(Version version, StatusCode statusCode, ReasonPhrase reasonPhrase, List<MessageHeader> messageHeaders) {
		Response response = new Response(version, statusCode, reasonPhrase);
		this.addDefaultMessageHeaders(response);
		response.addMessageHeaders(messageHeaders);
		return response;
	}
	
	/**
	 * Creates a HTTP response corresponding to the given parameters.
	 * It adds automatically the predefined message headers "Date", "Cache-Control" and "Connection".
	 * The given list of message headers is added after the predefined message headers.
	 * Thereby already existing headers will be overwritten.
	 * In the end corresponding to the message body the message headers "Content-Type" and "Content-Length" are added too.
	 * 
	 * @param version The HTTP version.
	 * @param statusCode The HTTP status code.
	 * @param reasonPhrase The HTTP reason phrase.
	 * @param messageHeaders A list of message headers.
	 * @param messageBody The HTTP message body.
	 * @return The corresponding HTTP response.
	 */
	public Response createResponse(Version version, StatusCode statusCode, ReasonPhrase reasonPhrase, List<MessageHeader> messageHeaders, MessageBody messageBody) {
		Response response = new Response(version, statusCode, reasonPhrase);
		this.addDefaultMessageHeaders(response);
		response.addMessageHeaders(messageHeaders);
		response.addMessageBody(messageBody);
		response.addMessageHeader(new MessageHeader("Content-Type", messageBody.getContentType().toString()));
		response.addMessageHeader(new MessageHeader("Content-Length", Integer.toString(messageBody.getLength())));
		return response;
	}
	
	/**
	 * Adds the predefined default headers "Date", "Cache-Control" and "Connection" to the given HTTP response.
	 * 
	 * @param response The HTTP response.
	 */
	private void addDefaultMessageHeaders(Response response) {
		response.addMessageHeader(new MessageHeader("Date", this.simpleDateFormat.format(new Date())));
		response.addMessageHeader(new MessageHeader("Cache-Control", "no-store"));
		response.addMessageHeader(new MessageHeader("Connection", "Close"));
	}

}
